<template>
  <div class="password-edit">
    <!-- 引入公共头部 -->
    <NavBar title="修改密码" back/>
    <div class="editPassword-form">
      <div class="editPassword-boder">
        <div class="regist-icon regist-password"></div>
        <input class="regist-input regist-pwd" :type="type" @change="checkOldPassword" v-model="oldpassword"  placeholder="旧密码"/>
        <div class="regist-hide" :class="{'regist-show':display}" @click="handelShow"></div>
      </div>
      <div class="editPassword-boder">
        <div class="regist-icon regist-password"></div>
        <input class="regist-input regist-pwd" :type="type" @change="checkNewPassword"  v-model="newpassword"  placeholder="新密码"/>
        <div class="regist-hide" :class="{'regist-show':display}" @click="handelShow"></div>
      </div>
      <div class="editPassword-boder">
        <div class="regist-icon regist-password"></div>
        <input class="regist-input regist-pwd" :type="type" @change="checkConfirmPassword" v-model="confirmpassword"  placeholder="确认密码"/>
        <div class="regist-hide" :class="{'regist-show':display}" @click="handelShow"></div>
      </div>
      <div class="editPassword-boder">
        <div class="regist-icon regist-code"></div>
        <input class="regist-input regist-codes" type="number" @change="checkCode" v-model="code" id="code" placeholder="短信验证码"/>
        <div class="regist-send" v-text="msg" @click="handelSend" disab>发送验证码</div>
      </div>
      <div class="regist-btn" @click="editPassword">立即修改</div>
    </div>
  </div>
</template>
<script>
import NavBar from '../../components/common/NavBar.vue'
import { Toast } from 'vant'
import editPasswordApi from '../../api/user'
export default {
  name: 'editPassword',
  data () {
    return {
      code: '',
      oldpassword: '',
      newpassword: '',
      confirmpassword: '',
      type: 'password',
      display: false,
      checked: true,
      send: false,
      msg: '发送验证码',
      cdisabel: false
    }
  },
  components: {
    NavBar
  },
  methods: {
    checkOldPassword: function () {
      if (this.oldpassword.length < 6 || this.oldpassword.length > 10) {
        Toast.fail('密码长度须在6-10位之间')
      }
    },
    checkNewPassword: function () {
      if (this.newpassword.length < 6 || this.newpassword.length > 10) {
        Toast.fail('密码长度须在6-10位之间')
      }
    },
    checkConfirmPassword: function () {
      if (this.newpassword !== this.confirmpassword) {
        Toast.fail('确认密码须和新密码一致')
      }
    },
    checkCode: function () {
      if (this.code === '') {
        Toast.fail('验证码不能为空')
      }
    },
    handelShow: function () {
      if (!this.display) {
        this.type = 'text'
      } else {
        this.type = 'password'
      }
      this.display = !this.display
    },
    handelSend: function () {
      if (this.oldpassword.length < 6 || this.oldpassword.length > 10) {
        Toast.fail('密码长度须在6-10位之间')
        return false
      }
      if (this.newpassword.length < 6 || this.newpassword.length > 10) {
        Toast.fail('密码长度须在6-10位之间')
        return false
      }
      if (this.newpassword !== this.confirmpassword) {
        Toast.fail('确认密码须和新密码一致')
        return false
      }
      const _this = this
      if (this.cdisabel) {
        return 0
      }
      editPasswordApi.sendCode(this.oldpassword, this.newpassword, this.confirmpassword).then(function (res) {
        if (res.data.success) {
          if (!_this.send) {
            let sec = 60
            _this.cdisabel = true
            for (let i = 0; i <= 60; i++) {
              window.setTimeout(function () {
                if (sec !== 0) {
                  _this.msg = sec + '秒后重发'
                  sec--
                } else {
                  _this.cdisabel = false
                  _this.msg = '发送验证码'
                }
              }, i * 1000)
            }
          }
          Toast.success(res.data.re)
        } else {
          _this.cdisabel = false
          Toast.fail(res.data.msg)
        }
      })
    },
    editPassword: function () {
      if (this.oldpassword.length < 6 || this.oldpassword.length > 10) {
        Toast.fail('密码长度须在6-10位之间')
        return false
      }
      if (this.newpassword.length < 6 || this.newpassword.length > 10) {
        Toast.fail('密码长度须在6-10位之间')
        return false
      }
      if (this.newpassword !== this.confirmpassword) {
        Toast.fail('确认密码须和新密码一致')
        return false
      }
      let _this = this
      editPasswordApi.editMemberPassword(this.code, this.oldpassword, this.newpassword, this.confirmpassword).then(function (res) {
        if (res.data.success) {
          Toast.success(res.data.re)
          _this.$store.dispatch('Logout', _this.loginForm).then(response => {
            _this.$router.push({ path: _this.redirect || '/login' })
          })
        } else {
          Toast.fail(res.data.msg)
        }
      })
    }
  }
}
</script>
<style>
  .password-edit {
    position: absolute;
    height: 100%!important;
    background: #fff;
  }
  .editPassword-boder {
    width: 90%;
    height: 40px;
    margin-top: 10px;
    margin-left: 5%;
    line-height: 40px;
    text-align: center;
    border-bottom: 1px solid #1ABC9C;
  }
  .regist-icon {
    height: 28px;
    line-height: 28px;
    width: 10%;
    float: left;
    text-align: right;
  }
  .regist-password {
    background-image: url('../../../static/images/login/password.png');
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: 5px 10px;
  }
  .regist-code {
    background-image: url('../../../static/images/login/code.png');
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: 5px 10px;
  }

  .regist-input {
    width: 80%;
    float: left;
    margin-left: 5%;
    height: 28px;
    line-height: 28px;
    border: 0px;
    color: #333333;
    font-size: 14px;
  }

  .regist-pwd {
    width: 70%;
  }

  .regist-hide {
    height: 28px;
    line-height: 28px;
    width: 10%;
    float: right;
    text-align: right;
    background-image: url('../../../static/images/login/hide.png');
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: 5px 10px;
  }

  .regist-show {
    background-image: url('../../../static/images/login/show.png');
  }

  .regist-input::-webkit-input-placeholder {
    color: #cccccc;
    font-size: 12px;
  }

  .regist-btn {
    width: 90%;
    height: 40px;
    line-height: 40px;
    margin-left: 5%;
    text-align: center;
    margin-top: 30px;
    font-size: 14px;
    font-weight: bold;
    border-radius: 5px;
    color: #ffffff;
    display: inline-block;
    background-color: #1ABC9C;
  }

  .regist-codes {
    width: 50%;
  }

  .regist-send {
    width: 30%;
    height: 37px;
    line-height: 37px;
    float: right;
    text-align: right;
    font-size: 12px;
    color: #888;
  }
</style>
